package com.beagle.njddapp.weight.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.amap.api.services.core.PoiItem;
import com.beagle.njddapp.R;
import com.beagle.njddapp.bean.owner.NjBean;
import com.beagle.njddapp.core.LocationManager;
import com.beagle.njddapp.core.UserService;
import com.blankj.utilcode.util.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author hxx
 * 输入加油量
 */
public class OilDIalog extends Dialog {

    @BindView(R.id.jiayou_count)
    EditText tvCount;
    private int orderId;

    public void setOrderId(int orderId) {
        this.orderId = orderId;
        tvCount.setText("");
    }

    private OilDialogHandleCallback oilDialogHandleCallback;

    public void setOilDialogHandleCallback(OilDialogHandleCallback oilDialogHandleCallback) {
        this.oilDialogHandleCallback = oilDialogHandleCallback;
    }

    public OilDIalog(@NonNull Context context) {
        super(context);
    }

    @Override
    public void show() {
        super.show();
        tvCount.setText("");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_oil);
        ButterKnife.bind(this);
        if (getWindow() != null) {
            //按空白处不能取消动画
            setCanceledOnTouchOutside(true);
            //设置window背景，默认的背景会有Padding值，不能全屏。当然不一定要是透明，你可以设置其他背景，替换默认的背景即可。
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            //一定要在setContentView之后调用，否则无效
            getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            getWindow().setWindowAnimations(R.style.DialogAnim);
        }
        tvCount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(".")){
                    if (s.length()-1-s.toString().indexOf(".")>3){
                        s=s.toString().subSequence(0,s.toString().indexOf(".")+4);
                        tvCount.setText(s);
                        tvCount.setSelection(s.length());
                    }
                }

                if(s.toString().trim().substring(0).equals(".")){
                    s ="0"+s;
                    tvCount.setText(s);
                    tvCount.setSelection(2);
                }

                if(s.toString().startsWith("0")
                        && s.toString().trim().length()>4){
                    if(!s.toString().substring(1,3).equals(".")){
                        tvCount.setText(s.subSequence(0,4));
                        tvCount.setSelection(3);
                        return;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }

        });

    }

    @OnClick({R.id.tv_cancle,R.id.tv_ok})
    public void onCLick(View view){
        switch (view.getId()){
            case R.id.tv_cancle:
                dismiss();
                break;
            case R.id.tv_ok:
                if (TextUtils.isEmpty(tvCount.getText().toString())){
                    ToastUtils.showShort("请填写加油量");
                    return;
                }
                oilDialogHandleCallback.oilOk(orderId,tvCount.getText().toString());
                dismiss();
                break;
        }
    }
    public interface  OilDialogHandleCallback{
        void oilOk(int orderId,String count);
    }
}
